<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="560px"
    @open="handleDialogOpen()" v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="goodsTypeModel" :rules="formgoodsTypes"
      ref="GoodsTypeForm" label-width="80px" label-position="right" size="small">
      <el-form-item label="货品类型名称" prop="name" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="goodsTypeModel.name" placeholder="请输入货品类型"></el-input></el-col>
      </el-form-item>
      <el-form-item label="运输类型" prop="goodsTypeModel.truckType.name" label-width="120px">
        <el-col :span="16">
           <el-select style="width:100%" clearable>
            <el-option
                    v-for="item in typeoptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
            </el-option>
          </el-select>
          </el-col>
      </el-form-item>
       <el-form-item label="默认体积(方)" prop="defaultVolume" label-width="120px">
        <el-col :span="16">
         <el-input :maxlength="32" v-model="goodsTypeModel.defaultVolume" placeholder="请输入默认体积(方)"></el-input>
        </el-col>
      </el-form-item>
       <el-form-item label="默认重量(千克)" prop="defaultWeight" label-width="120px">
        <el-col :span="16"><el-input :maxlength="32" v-model="goodsTypeModel.defaultWeight" placeholder="请输入默认重量(千克)"></el-input></el-col>
      </el-form-item>
      <el-form-item label="备注" prop="remark" label-width="120px">
        <el-col :span="30"><textarea v-model="goodsTypeModel.remark"></textarea></el-col>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('GoodsTypeForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import goodsApi from "@/api/goods.js";
export default {
   name: "goodsType-edit",
  //  props: ['parentTruckTypeNameList'],
    data() {
    return {
      loading: false,
      goodsTypeModel: {
        name:"",
        empname: '',
        defaultVolume: '',
        defaultWeight:'',
        remark: '',
      },
      truckTypeName:[],
      dialogProps: {
        visible: false,
        action: '',
        title: ''
      },
      formgoodsTypes: {
      },
      typeoptions:[]
    }
 },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.dialogProps.action === 'add') {
            this.doSaveGoodsType()
          } else {
            // this.doUpdateEmp()
          }
        } else {
          return false
        }
      })
    },
    getItem(val){
        let obj={}
        obj=this.truckTypeName.find(item=>{
          return item.id==val;
        })
        console.log(obj.id)
    },
    // doUpdateEmp() {
    //   this.loading = true
    //   updateEmp(this.EmpModel)
    //   .then(response => {
    //     this.loading = false
    //     this.dialogProps.visible = false
    //     this.$emit('save-finished')
    //   })
    //   .catch(error => {
    //     this.outputError(error)
    //   })
    // },
    doSaveGoodsType() {
      this.loading = true
      console.log(this.GoodsTypeForm)
      axios.post("",this.GoodsTypeForm)
      .then(res => {
        console.log(res)
      })
      .catch(err => {
        console.error(err);
      })
      // addEmp(this.EmpModel)
      // .then(response => {
      //   console.log(response.data)
      //   this.loading = false
      //   this.dialogProps.visible = false
      //   this.$emit('save-finished')
      // })
      // .catch(error => {
      //   this.outputError(error)
      // })
    },
    handleDialogClose() {
      this.dialogProps.visible = false
    },
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs['GoodsTypeForm'].clearValidate()
      })
    },
    outputError(error) {
      console.log(error.response ? error.response : error.message)
      this.loading = false
      this.$message({
        showClose: true,
        message: '出错了，请按F12查看浏览器日志。',
        type: 'error'
      })
    }
  },
   mounted: function() {
    this.$nextTick(() => {
      this.$on('openEditGoodsTypeDialog', function() {
        // this.GoodsTypeForm = emp
        this.dialogProps.action = 'edit'
        this.dialogProps.title = '编辑货品类型'
        this.dialogProps.visible = true
      })
      this.$on('openAddGoodsTypeDialog', function() {
        this.dialogProps.action = 'add'
        this.dialogProps.title = '新增货品类型'
        this.dialogProps.visible = true
      })
    })
  },
  created(){
      // 查询运输车辆类型
        goodsApi.queryTruckType().then(res =>{
            this.typeoptions = res.data
        })
    },
}
</script>

<style>

</style>
